package com.ctg.itrdc.sysmgr.portal.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import com.ctg.itrdc.pasp.context.service.BaseService;
import com.ctg.itrdc.sysmgr.portal.api.dto.CommonRegionDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.ICommonRegionService;
import com.ctg.itrdc.sysmgr.portal.common.constant.DictEnum;
import com.ctg.itrdc.sysmgr.portal.data.ICommonRegionDao;
import com.ctg.itrdc.sysmgr.portal.data.entity.CommonRegion;
import com.ctg.itrdc.sysmgr.portal.service.exception.SysmgrServiceException;
import com.ctg.itrdc.sysmgr.portal.service.utils.BeanUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@SuppressWarnings("unchecked")
@Component("commonRegionService")
public class CommonRegionServiceImpl extends
		BaseService<CommonRegion, Long, CommonRegionDTO> implements ICommonRegionService ,DictEnum{

	@Resource
	private ICommonRegionDao commonRegionDao;

	@Override
	public Long insert(CommonRegionDTO dto) {
		CommonRegionDTO tmp = new CommonRegionDTO();
		tmp.setRegionName(dto.getRegionName());
		tmp.setParRegionId(dto.getParRegionId());
		if (this.exists(tmp)) {
			throw new SysmgrServiceException("同父级，子区域不能同名！");
		}
		return super.insert(dto);
	}

	@Override
	public Map<String, Map<String, Object>> selectEditData(CommonRegionDTO dto) {
		Map<String, Map<String, Object>> map = Maps.newHashMap();
		dto = this.selectByPrimaryKey(dto);
		recursiveSetEditData(map, dto);
		setSubEditData(dto, map);
		
		return map;
	}

	private void setSubEditData(CommonRegionDTO dto,
			Map<String, Map<String, Object>> map) {
		CommonRegionDTO tmp = new CommonRegionDTO();
		tmp.setParRegionId(dto.getCommonRegionId());
		tmp.setStatusCd(dto.getStatusCd());
		List<CommonRegionDTO> subList = this.selectByExample(tmp);
		if(CollectionUtils.isNotEmpty(subList)){
			Map<String, Object> subMap = Maps.newHashMap();
			subMap.put("data", subList);
			map.put(subList.get(0).getRegionType(), subMap);
		}
	}

	private void recursiveSetEditData(Map<String, Map<String, Object>> map,
			CommonRegionDTO param) {

		if (param == null)
			return;
		
		setEditData(map, param);
		Long parRegionId = param.getParRegionId();
		if (parRegionId != null && parRegionId != 0) {
			CommonRegionDTO dto = new CommonRegionDTO();
			dto.setId(parRegionId);
			dto = this.selectByPrimaryKey(dto);
			recursiveSetEditData(map, dto);
		}
	}

	private void setEditData(Map<String, Map<String, Object>> map,
			CommonRegionDTO param) {
		
		CommonRegionDTO tmp = new CommonRegionDTO();
		tmp.setParRegionId(param.getParRegionId());
		tmp.setStatusCd(param.getStatusCd());
		
		List<CommonRegionDTO> dtoList = this.selectByExample(tmp);
		Map<String, Object> innerMap = Maps.newHashMap();
		innerMap.put("select", param.getId());
		innerMap.put("data", dtoList);
		map.put(param.getRegionType(), innerMap);
	}

	// @Override
	// public List<CommonRegionDTO> selectRegionTree(){
	// List<CommonRegionDTO> dtoList = this.selectByExample(new
	// CommonRegionDTO());
	// return selectRegionTree(dtoList);
	// }

	@Override
	public List<CommonRegionDTO> selectSubRegion(CommonRegionDTO dto) {
		CommonRegion entity = BeanUtils.applyIfNotException(dto,
				CommonRegion.class);
		List<CommonRegionDTO> resultList = Lists.newArrayList();
		List<CommonRegion> entityList = commonRegionDao.selectSubRegion(entity);
		for (CommonRegion commonRegion : entityList) {
			Long commonRegionId = commonRegion.getCommonRegionId();
			CommonRegionDTO tmp = new CommonRegionDTO();
			tmp.setParRegionId(commonRegionId);
			tmp.setStatusCd(dto.getStatusCd());
			CommonRegionDTO result = BeanUtils.applyIfNotException(commonRegion,
					CommonRegionDTO.class);
			if (this.exists(tmp)) {
				result.setHasChildren(true);
			} else {
				result.setHasChildren(false);
			}
			resultList.add(result);
		}
		return resultList;
	}

	// private List<CommonRegionDTO> selectRegionTree(List<CommonRegionDTO>
	// dtoList){
	// List<CommonRegionDTO> result = Lists.newArrayList();
	// for (CommonRegionDTO main : dtoList) {
	// if(main.getParRegionId()==null || main.getParRegionId() == 0){
	// selectSubRegion(main, dtoList);
	// result.add(main);
	// }
	// }
	// return result;
	// }
	//
	// private void selectSubRegion(CommonRegionDTO main,List<CommonRegionDTO>
	// dtoList){
	// List<CommonRegionDTO> item = Lists.newArrayList();
	// for (CommonRegionDTO sub : dtoList) {
	// Long parentId =
	// sub.getParRegionId()==null?null:sub.getParRegionId().longValue();
	// if(parentId==main.getCommonRegionId()){
	// selectSubRegion(sub,dtoList);
	// item.add(sub);
	// }
	// }
	// if(item.size()>0)
	// main.setItem(item);
	// }
}
